const { osName } = uni.getSystemInfoSync()
/** 是否IOS环境 */
const isIOS = osName === 'ios'
/** 是否安卓环境 */
const isAndroid = osName === 'android'
/** 是否微信环境 */
let isWeChat = false

export { isIOS, isAndroid, isWeChat }

/** 小程序环境 */
const miniProgramEnv = {
  appId: '',
  version: '',
  env: '',
  isDevelop: false,
  isTrial: false,
  isRelease: false,
  notDevelop: false,
  notTrial: false,
  notRelease: false,
}

// #ifdef MP-WEIXIN
const { miniProgram } = uni.getAccountInfoSync()

miniProgramEnv.appId = miniProgram.appId
miniProgramEnv.version = miniProgram.version
miniProgramEnv.env = miniProgram.envVersion
miniProgramEnv.isDevelop = miniProgram.envVersion === 'develop'
miniProgramEnv.isTrial = miniProgram.envVersion === 'trial'
miniProgramEnv.isRelease = miniProgram.envVersion === 'release'

isWeChat = true
// #endif

// #ifdef MP-TOUTIAO
// @ts-ignore
const { microapp } = uni.getEnvInfoSync()

miniProgramEnv.appId = microapp.appId
miniProgramEnv.version = microapp.mpVersion
miniProgramEnv.env = microapp.envType
miniProgramEnv.isDevelop = microapp.envType === 'development'
miniProgramEnv.isTrial = microapp.envType === 'preview'
miniProgramEnv.isRelease = microapp.envType === 'production'
// #endif

miniProgramEnv.notDevelop = !miniProgramEnv.isDevelop
miniProgramEnv.notTrial = !miniProgramEnv.isRelease
miniProgramEnv.notRelease = !miniProgramEnv.isRelease

export { miniProgramEnv }

let isWeixinJSBridgeReady = false

// #ifdef WEB
isWeChat = /MicroMessenger/i.test(window.navigator.userAgent)

if (isWeChat) {
  const cb = () => (isWeixinJSBridgeReady = true)

  if (document.addEventListener) {
    document.addEventListener('WeixinJSBridgeReady', cb, false)
  } else if ((document as any).attachEvent) {
    ;(document as any).attachEvent('WeixinJSBridgeReady', cb)
    ;(document as any).attachEvent('onWeixinJSBridgeReady', cb)
  }
}
// #endif

export { isWeixinJSBridgeReady }
